package com.vessel.decoration.mvp.presenter;

import android.app.Application;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.RxLifecycleUtils;
import com.vessel.decoration.app.data.entity.ServiceListResponse;
import com.vessel.decoration.mvp.contract.SearchContract;
import com.vessel.decoration.mvp.ui.activity.artisan.ArtisanActivity;
import com.vessel.decoration.mvp.ui.activity.shop.ShopActivity;
import com.vessel.decoration.mvp.ui.adapter.ArtusanListAdapter;
import com.vessel.decoration.mvp.ui.adapter.ShopListAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

/**
 * @author Jimq
 * @date 2017/12/8
 */
@ActivityScope
public class SearchPresenter extends BasePresenter<SearchContract.Model, SearchContract.View> implements AdapterView.OnItemClickListener {
    private RxErrorHandler mErrorHandler;
    private AppManager mAppManager;
    private Application mApplication;
    private int index = 1;
    private ShopListAdapter mAdapter;
    private ArtusanListAdapter mAdapter2;
    private List<ServiceListResponse.ShopsBean> mShopList;
    private List<ServiceListResponse.ArtisansBean> mArtisansList;
    private int mType;

    @Inject
    public SearchPresenter(SearchContract.Model model, SearchContract.View rootView, RxErrorHandler handler
            , AppManager appManager, Application application) {
        super(model, rootView);
        this.mApplication = application;
        this.mErrorHandler = handler;
        this.mAppManager = appManager;
        mShopList = new ArrayList<>();
        mArtisansList = new ArrayList<>();
    }

    public void getSearchResult(boolean pullToRefresh, String keywords, int type) {
        mType = type;
        if (pullToRefresh) index = 1;//上拉刷新默认只请求第一页
        Map<String, Object> map = new HashMap<>();
        map.put("keyword", keywords);
        map.put("type", type);
        map.put("page", index);
        map.put("pageSize", 10);
        mModel.queryServiceList(map)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<ServiceListResponse>(mErrorHandler) {
                    @Override
                    public void onNext(ServiceListResponse serviceListResponse) {
                        switch (mType) {
                            case 0:
                                if (mAdapter2 == null) {
                                    mAdapter2 = new ArtusanListAdapter();
                                    mRootView.setAdapter(mAdapter2);
                                }
                                if (serviceListResponse == null || serviceListResponse.getArtisans() == null || serviceListResponse.getArtisans().size() == 0) {
                                    mRootView.showMessage("暂无搜索结果");
                                    return;
                                }
                                index += 1;
                                if (pullToRefresh) {
                                    mArtisansList.clear();
                                    mArtisansList.addAll(serviceListResponse.getArtisans());
                                    mRootView.updateLoading(true);
                                    mAdapter2.setList(serviceListResponse.getArtisans());
                                    mAdapter2.notifyDataSetChanged();
                                } else {
                                    mArtisansList.addAll(serviceListResponse.getArtisans());
                                    mRootView.updateLoading(false);
                                    mAdapter2.addList(serviceListResponse.getArtisans());
                                    mAdapter2.notifyDataSetChanged();
                                }
                                break;
                            case 1:
                                if (mAdapter == null) {
                                    mAdapter = new ShopListAdapter();
                                    mRootView.setAdapter(mAdapter);
                                }
                                if (serviceListResponse == null || serviceListResponse.getShops() == null || serviceListResponse.getShops().size() == 0) {
                                    mRootView.showMessage("暂无搜索结果");
                                    return;
                                }
                                index += 1;
                                if (pullToRefresh) {
                                    mShopList.clear();
                                    mShopList.addAll(serviceListResponse.getShops());
                                    mRootView.updateLoading(true);
                                    mAdapter.setList(serviceListResponse.getShops());
                                    mAdapter.notifyDataSetChanged();
                                } else {
                                    mShopList.addAll(serviceListResponse.getShops());
                                    mRootView.updateLoading(false);
                                    mAdapter.addList(serviceListResponse.getShops());
                                    mAdapter.notifyDataSetChanged();
                                }
                                break;

                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mApplication = null;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (mType) {
            case 0:
                if (mArtisansList == null || mArtisansList.size() == 0) {
                    return;
                }
                Intent intent = new Intent(mApplication, ArtisanActivity.class);
                intent.putExtra("artisanId", mArtisansList.get(i).getArtisanId());
                ArmsUtils.startActivity(intent);
                break;
            case 1:
                if (mShopList == null || mShopList.size() == 0) {
                    return;
                }
                Intent intent2 = new Intent(mApplication, ShopActivity.class);
                intent2.putExtra("shopId", mShopList.get(i).getShopId());
                ArmsUtils.startActivity(intent2);
                break;

        }

    }
}
